package com.course.action;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.course.bean.AnnouncementTo;
import com.course.delegate.CourseMgrDelegate;
import com.course.exception.ConnectionException;
import com.course.exception.LoginException;
import com.course.formbean.AnnouncementBean;
import com.course.util.UtilConstants;



public class AnnouncementAction extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		AnnouncementBean ab=new AnnouncementBean();
		
		
		Map map=request.getParameterMap();
	try {
		BeanUtils.populate(ab, map);
	} catch (IllegalAccessException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (InvocationTargetException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		
		
	}
        String path="";
        boolean flag=false;

	  try{
		  
		  String login=(String) request.getSession().getAttribute("user");
		  
		  System.out.println("in action Class login is........."+login);
		  
		  ab.setLogin(login);
		  
		  AnnouncementTo ato=new AnnouncementTo(ab);
		  try{
		  
		 flag= new CourseMgrDelegate().insertAnnouncement(ato);				  								 
		  
		  }catch (ConnectionException e) {
			e.printStackTrace();
		}catch (LoginException e) {
			e.printStackTrace();
		}
		  System.out.println("in Action class vcb..........."+flag);
		  if(flag){
			  request.setAttribute("status",UtilConstants._ADD_ANNOUNCEMENT_SUCCESS );
			  path=UtilConstants._ADD_ANNOUNCEMENT_HOME;
			 }
		  else {
			  request.setAttribute("status",UtilConstants._ADD_ANNOUNCEMENT_FAILURE );
			  path=UtilConstants._ADD_ANNOUNCEMENT_HOME;
			}
		  
   }
	  catch (Exception e) {
		  request.setAttribute("status",UtilConstants._INVALIED_DATA );
		  path=UtilConstants._ADD_ANNOUNCEMENT_HOME;
		e.printStackTrace();
		
	}
     RequestDispatcher rd=request.getRequestDispatcher(path);
		rd.forward(request,response);
		}

}
